package com.example.yinzradio.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@EnableScheduling
@Slf4j
public class SchedulService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RadiaoService radiaoService;
    @Autowired
    private RadioMapper radioMapper;

    String path = "C:\\repairRadio";
    String wavName = "out";


    //绕线广播
//    @Scheduled(cron = "0/3 * * * * ?")
//    public void scheRadio() throws IOException, InterruptedException {
//
////        RadioSetting radioSetting = radioMapper.selectOne(new QueryWrapper<RadioSetting>()
////                .eq("radio_dept","绕线车间").eq("status",1).notIn("dept_line","印字班").last("limit 1"));
////
//        List<RadioSetting> radioSettings = radioMapper.selectList(new QueryWrapper<RadioSetting>().eq("radio_dept","绕线车间").eq("status",1).notIn("dept_line","印字班"));
//        RadioSetting radioSetting = new RadioSetting();
//        if (radioSettings.size()>0){
//            radioSetting = radioSettings.get(0);
//            radioSetting.setStatus(2);
//            radioMapper.updateById(radioSetting);
//            TextToVoice.textToSpeech(radioSetting.getRadioContent(),path,wavName);
//            String dd = path+"\\dd.wav";
//            radiaoService.play(dd);
//            Thread.sleep(1500);
//            String playAddr = path+"\\"+wavName+".wav";
//            radiaoService.play(playAddr);
//        }
//    }

//一部
    @Scheduled(cron = "0/3 * * * * ?")
    public void scheRadio() throws IOException, InterruptedException {
        List<RadioSetting> radioSettings = radioMapper.selectList(new QueryWrapper<RadioSetting>()
                .eq("radio_dept","制造一部").eq("status",1));
        RadioSetting radioSetting = new RadioSetting();
        if (radioSettings.size()>0){
            radioSetting = radioSettings.get(0);
            radioSetting.setStatus(2);
            radioMapper.updateById(radioSetting);
            TextToVoice.textToSpeech(radioSetting.getRadioContent(),path,wavName);
            String dd = path+"\\dd.wav";
            radiaoService.play(dd);
            Thread.sleep(1500);
            String playAddr = path+"\\"+wavName+".wav";
            radiaoService.play(playAddr);
        }
    }
}
